package cn.com.user.controller;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cabinet.base.util.PropertiesFileUtil;
import com.cabinet.web.entity.Cabinet;

import cn.com.cabinet.dao.CabinetDao;
import cn.com.easy.utils.CookieUtils;

@Controller
@RequestMapping()
public class IndexController {
	
	@Autowired
	private CabinetDao cabinetDao;
	
	@RequestMapping()
	public String index(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String userid = CookieUtils.getCookieValue(request, "userid");
		if(userid == "null" || userid == null){
			return "redirect:"+getWechatAutoLoginUrl();
		}else{
			if(CookieUtils.getCookieValue(request, "cid") == null || CookieUtils.getCookieValue(request, "cid").equals("null")){
				return "redirect:/near/by";
			}else{
				return "redirect:/index/"+CookieUtils.getCookieValue(request, "cid");
			}
		}
	}
	
	@RequestMapping("/index/{id}")
	public String redirectindex(@PathVariable String id,HttpServletRequest request, HttpServletResponse response,ModelMap modelMap) throws Exception{
		CookieUtils.setCookie(request, response, "cid",id);
		String userid = CookieUtils.getCookieValue(request, "userid");
		if(userid == "null" || userid == null){
			return "redirect:"+getWechatAutoLoginUrl();
		}else{
			Cabinet c = cabinetDao.getCabinetByCid(id);
			if(c == null){
				return "redirect:/near/by";
			}else{
				int cclass = Integer.valueOf(CookieUtils.getCookieValue(request, "cid").substring(0, 1));
				modelMap.addAttribute("cclass", cclass);
				return "/index/index";
			}
		}
	}
	
	@RequestMapping("/index")
	public String testIndex(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String userid = CookieUtils.getCookieValue(request, "userid");
		if(userid == "null" || userid == null){
			return "redirect:"+getWechatAutoLoginUrl();
		}else{
			if(CookieUtils.getCookieValue(request, "cid") == null || CookieUtils.getCookieValue(request, "cid").equals("null")){
				return "redirect:/near/by";
			}else{
				return "redirect:/index/"+CookieUtils.getCookieValue(request, "cid");
			}
		}		
	}
	
	
	@RequestMapping("/QR/{id}")
	public String QRIndex(@PathVariable String id,HttpServletRequest request, HttpServletResponse response) throws Exception{
		CookieUtils.setCookie(request, response, "cid",id);
		String wechatOauth = PropertiesFileUtil.getValue(PropertiesFileUtil.defaultFileName, "wechat.oauth");
		String appid = PropertiesFileUtil.getValue("/wechat4j", "wechat.appid");
		String wechatUrl = PropertiesFileUtil.getValue("/wechat4j", "wechat.url");
		String url = String.format(wechatOauth, appid,wechatUrl+"/wechat/login");
		return "redirect:"+url;
	}
	
	
	@RequestMapping("/QR/test/{id}")
	public String QRIndexTest(@PathVariable String id,HttpServletRequest request, HttpServletResponse response) throws Exception{
		CookieUtils.setCookie(request, response, "cid",id);
		return "redirect:/wechat/QR/test";
	}

	
	public String getWechatAutoLoginUrl() throws IOException{
		
		String wechatOauth = PropertiesFileUtil.getValue(PropertiesFileUtil.defaultFileName, "wechat.oauth");
		String appid = PropertiesFileUtil.getValue("/wechat4j", "wechat.appid");
		String wechatUrl = PropertiesFileUtil.getValue("/wechat4j", "wechat.url");
		String url = String.format(wechatOauth, appid,wechatUrl+"/wechat/login");
		return url;
	}
}
